package com.itealabs.postjson.ext;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 
 * @version $Id: ThreadPoolFactory.java 12 2010-04-22 16:01:15Z iteahere $
 * @author itea
 * 2010-4-22
 */
public class ThreadPoolFactory {

	private int corePoolSize;
	private int maximumPoolSize;
	private long keepAliveTime;
	private BlockingQueue<Runnable> workQueue;
	private ExecutorService executorService;
	
	@PostConstruct
	public synchronized void init() {
		if(executorService == null)
			executorService = new ThreadPoolExecutor(corePoolSize, 
				maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue);
	}
	
	@PreDestroy
	public synchronized void destroy() {
		if(executorService != null) {
			executorService.shutdown();
			executorService = null;
		}
	}
	
	public int getCorePoolSize() {
		return corePoolSize;
	}
	public void setCorePoolSize(int corePoolSize) {
		this.corePoolSize = corePoolSize;
	}
	public int getMaximumPoolSize() {
		return maximumPoolSize;
	}
	public void setMaximumPoolSize(int maximumPoolSize) {
		this.maximumPoolSize = maximumPoolSize;
	}
	public long getKeepAliveTime() {
		return keepAliveTime;
	}
	public void setKeepAliveTime(long keepAliveTime) {
		this.keepAliveTime = keepAliveTime;
	}
	public BlockingQueue<Runnable> getWorkQueue() {
		return workQueue;
	}
	public void setWorkQueue(BlockingQueue<Runnable> workQueue) {
		this.workQueue = workQueue;
	}
	public ExecutorService getExecutorService() {
		return executorService;
	}
}
